package it.win4buy.services
{
    import flash.events.IEventDispatcher;

    import it.win4buy.events.GetQuestionEvent;
    import it.win4buy.utility.ApplicationConstants;

    import mx.rpc.events.FaultEvent;
    import mx.rpc.events.ResultEvent;
    import mx.rpc.remoting.RemoteObject;

    /**
     *
     *	autore:
     *  pierangelo.mancusi@gmail.com
     *
     */

    public class GetQuestionService
    {



        //-------------------------------------------
        //
        // Injected properties
        //
        //-------------------------------------------
        [Dispatcher]
        public var dispatcher:IEventDispatcher;

        //-------------------------------------------
        //
        // Private Properties
        //
        //-------------------------------------------


        private var ro:RemoteObject;


        //-------------------------------------------
        //
        // Getters / Setters
        //
        //-------------------------------------------


        //-------------------------------------------
        //
        // Constructor
        //
        //-------------------------------------------

        public function GetQuestionService()
        {
        }

        //-------------------------------------------
        //
        // Public Methods
        //
        //-------------------------------------------

        public function execute(userId:String, placeHolderId:String, numQuestion:String):void
        {
            ro=new RemoteObject();

            ro.destination=ApplicationConstants.DESTINATION_TEST;

            ro.addEventListener(ResultEvent.RESULT, serviceResult);
            ro.addEventListener(FaultEvent.FAULT, serviceFault);

            ro.getQuestionData();

        }

        //-------------------------------------------
        //
        // Private Methods
        //
        //-------------------------------------------

        private function serviceResult(event:ResultEvent):void
        {

            ro.removeEventListener(ResultEvent.RESULT, serviceResult);
            ro.removeEventListener(FaultEvent.FAULT, serviceFault);
            ro=null;
            dispatcher.dispatchEvent(new GetQuestionEvent(GetQuestionEvent.RESULT, event.result, true));
        }

        private function serviceFault(event:FaultEvent):void
        {
            ro.removeEventListener(ResultEvent.RESULT, serviceResult);
            ro.removeEventListener(FaultEvent.FAULT, serviceFault);
            ro=null;

            dispatcher.dispatchEvent(new GetQuestionEvent(GetQuestionEvent.FAULT, event.message, true));
        }



    }
}